package com.abtu.sms.course.handler;

import com.abtu.sms.course.service.CourrseService;
import com.abtu.sms.pojo.Course;
import com.abtu.sms.pojo.SelectCourse;
import com.abtu.sms.util.CommonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseHandler {
    @Resource
    private CourrseService courrseService;

//    @PostMapping("")
//    @ResponseBody
//    public CommonResult getCourseBysicAndtid() {
//
//    }
    @PostMapping("/addcourse")
    public String addOptionalCourse(Course course, HttpServletRequest request) {
        course.setTid(request.getSession().getAttribute("tid").toString());
        try {
            int course_temp = courrseService.addOptionalCourse(course);
            if (1 == course_temp) {
                return "redirect:../course/selfcourse/" + (String)request.getSession().getAttribute("tid");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            return null;
        }
        return null;
    }

    @GetMapping ("/selfcourse/{tid}")
    public String toMypage(@PathVariable("tid") String tid, HttpServletRequest request) {
        try {
            List<Course> mySelfOptionalCourse = courrseService.findMySelfOptionalCourse(tid);
            request.getSession().setAttribute("cs", mySelfOptionalCourse);
            return "redirect:../../teacher/teacherOptionalCourse.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }
    @GetMapping ("/findstudentcourse/{cid}")
    public String toMystudent(@PathVariable("cid") String cid, HttpServletRequest request){
        try {
            List<SelectCourse> findStudnetCourse = courrseService.findStudentSelectedCourseByCourseId(cid);
            request.getSession().setAttribute("csv",findStudnetCourse);
            return "redirect:../../teacher/findSelectCourseListByCourseId.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }
    @GetMapping ("/findscore/{cid,sid}")
    public String findScoreByCourseIdAndStudentId(@PathVariable("cid,sid") String cid, String sid, HttpServletRequest request){
        try {
            List<SelectCourse> findStudnetScore = (List<SelectCourse>) courrseService.findScoreByCourseIdAndStudentId(cid,sid);
            request.getSession().setAttribute("csf",findStudnetScore);
            return "redirect:../../teacher/updateOptionalCourseScore.jsp";
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }

    @ResponseBody
    @GetMapping ("/updatescore/{cid}/{sid}/{score}")
    public String upDateScoreByCidAndSid(
            @PathVariable("cid") String cid,
            @PathVariable("sid") String sid,
            @PathVariable("score") String score,
            HttpServletRequest request){
        try {
            int i = courrseService.upDateScoreByCidAndSid(cid, sid, score);
//            request.getSession().setAttribute("csu",updateStudentScore);
            return i == 1 ? "yes" : "no";
        } catch (SQLException e) {
            e.printStackTrace();
            return "no";
        }
    }

    @GetMapping ("/deletecourse/{cid}")
    public String  deleteCourseById(@PathVariable("cid") String cid, HttpServletRequest request){
        try {
            int deleteStudnetCourse = courrseService.deleteCourseById(cid);
//            request.getSession().setAttribute("csd",deleteStudnetCourse);
            return "redirect:../selfcourse/"+request.getSession().getAttribute("tid");

//            course/selfcourse/<%=request.getSession().getAttribute("tid")
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }

}
